package org.apache.maven.plugin.car;

/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
import org.apache.maven.archiver.MavenArchiveConfiguration;
import org.apache.maven.archiver.MavenArchiver;
import org.apache.maven.artifact.DependencyResolutionRequiredException;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.project.MavenProject;
import org.apache.maven.shared.filtering.MavenFileFilter;
import org.apache.maven.shared.filtering.MavenFilteringException;
import org.apache.maven.shared.filtering.MavenResourcesExecution;
import org.codehaus.plexus.archiver.ArchiverException;
import org.codehaus.plexus.archiver.jar.JarArchiver;
import org.codehaus.plexus.archiver.jar.ManifestException;
import org.codehaus.plexus.util.FileUtils;
import org.codehaus.plexus.util.xml.XmlStreamReader;

import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * Build a JEE Application Client jar file from the current project.
 *
 * @author <a href="pablo@anahata-it.com">Pablo Rodriguez</a>
 * @author <a href="snicoll@apache.org">Stephane Nicoll</a>
 * @version $Id:
 * @goal car
 * @requiresDependencyResolution runtime
 * @threadSafe
 * @phase package
 */
public class CarMojo
        extends AbstractMojo {

    private static final String APP_CLIENT__XML = "META-INF/application-client.xml";
    // TODO: will null work instead?
    private static final String[] DEFAULT_INCLUDES = new String[]{"**/**"};
    private static final String[] DEFAULT_EXCLUDES = new String[]{APP_CLIENT__XML};
    private static final String[] EMPTY_STRING_ARRAY = new String[0];
    /**
     * The directory for the generated jar.
     *
     * @parameter default-value="${project.build.directory}"
     * @required
     * @readonly
     */
    private File basedir;
    /**
     * Directory that resources are copied to during the build.
     *
     * @parameter default-value="${project.build.outputDirectory}" expression="${outputDirectory}"
     */
    private File outputDirectory;
    /**
     * The name of the Application client JAR file to generate.
     *
     * @parameter default-value="${project.build.finalName}" expression="${jarName}"
     */
    private String jarName;
    /**
     * The files and directories to exclude from the main Application Client jar. Usage:
     *
     * <pre>
     * &lt;excludes&gt;
     *   &lt;exclude&gt;**&#47;*DevOnly.class&lt;&#47;exclude&gt;
     * &lt;&#47;excludes&gt;
     * </pre>
     * <br/>Default exclusions: META-INF&#47;application-client.xml,
     * @parameter
     */
    private List excludes;
    /**
     * The Maven project.
     *
     * @parameter default-value="${project}"
     * @required
     * @readonly
     */
    private MavenProject project;
    /**
     * The Jar archiver.
     *
     * @component role="org.codehaus.plexus.archiver.Archiver" roleHint="jar"
     */
    private JarArchiver jarArchiver;
    /**
     * What JEE version should the CAR Plugin generate? Valid values are "5" and "6".
     * <p/>
     * Usage:
     * <pre>
     * &lt;jeeVersion&gt;5&lt;&#47;jeeVersion&gt;
     * </pre>
     *
     * @parameter default-value="6" expression="${car.jeeVersion}"
     * @since 2.1
     */
    private String jeeVersion;

    /**
     * The archive configuration to use.
     * See <a href="http://maven.apache.org/shared/maven-archiver/index.html">Maven Archiver Reference</a>.
     * This version of the CAR Plugin uses Maven Archiver 2.4.
     *
     * @parameter
     */
    private MavenArchiveConfiguration archive = new MavenArchiveConfiguration();
    /**
     * To escape interpolated value with windows path.
     * c:\foo\bar will be replaced with c:\\foo\\bar.
     *
     * @parameter default-value="false" expression="${car.escapeBackslashesInFilePath}"
     * @since 2.3
     */
    private boolean escapeBackslashesInFilePath;
    /**
     * An expression preceded with this String won't be interpolated.
     * \${foo} will be replaced with ${foo}.
     *
     * @parameter expression="${car.escapeString}"
     * @since 2.3
     */
    protected String escapeString;
    /**
     * To filter the deployment descriptor.
     *
     * @parameter default-value="false" expression="${car.filterDeploymentDescriptor}"
     * @since 2.3
     */
    private boolean filterDeploymentDescriptor;
    /**
     * Filters (properties files) to include during the interpolation of the deployment descriptor.
     *
     * @parameter
     * @since 2.3
     */
    private List filters;
    /**
     * @component role="org.apache.maven.shared.filtering.MavenFileFilter" role-hint="default"
     * @required
     * @since 2.3
     */
    private MavenFileFilter mavenFileFilter;
    /**
     * @parameter expression="${session}"
     * @readonly
     * @required
     * @since 2.3
     */
    private MavenSession session;

    /**
     * Generates the application client jar file
     *
     * @todo Add license files in META-INF directory.
     */
    public void execute()
            throws MojoExecutionException {
        if (getLog().isInfoEnabled()) {
            getLog().info("Building JEE Application client " + jarName + " with JEE version " + jeeVersion);
        }

        File jarFile = getAppClientJarFile(basedir, jarName);

        MavenArchiver archiver = new MavenArchiver();

        archiver.setArchiver(jarArchiver);

        archiver.setOutputFile(jarFile);

        File deploymentDescriptor = new File(outputDirectory, APP_CLIENT__XML);

        /* test JEE version compliance */
        if (!jeeVersion.matches("\\A[5-6]\\z")) {
            throw new MojoExecutionException(
                    "jeeVersion is not valid: " + jeeVersion + ". Must be 5 or 6 ");
        }
        /*
        if ( ejbVersion.matches( "\\A2\\.[0-9]\\z" ) && !deploymentDescriptor.exists() )
        {
        throw new MojoExecutionException(
        "Error assembling EJB: " + EJB_JAR_XML + " is required for ejbVersion 2.x" );
        }*/

        try {
            String[] mainJarExcludes = DEFAULT_EXCLUDES;

            if (excludes != null && !excludes.isEmpty()) {
                excludes.add(APP_CLIENT__XML);
                mainJarExcludes = (String[]) excludes.toArray(EMPTY_STRING_ARRAY);
            }

            archiver.getArchiver().addDirectory(outputDirectory, DEFAULT_INCLUDES, mainJarExcludes);

            if (deploymentDescriptor.exists()) {
                if (filterDeploymentDescriptor) {
                    getLog().debug("Filtering deployment descriptor.");
                    MavenResourcesExecution mavenResourcesExecution = new MavenResourcesExecution();
                    mavenResourcesExecution.setEscapeString(escapeString);
                    List filterWrappers = mavenFileFilter.getDefaultFilterWrappers(project, filters,
                            escapeBackslashesInFilePath,
                            this.session,
                            mavenResourcesExecution);

                    // Create a temporary file that we can copy-and-filter
                    File unfilteredDeploymentDescriptor = new File(outputDirectory, APP_CLIENT__XML + ".unfiltered");
                    FileUtils.copyFile(deploymentDescriptor, unfilteredDeploymentDescriptor);
                    mavenFileFilter.copyFile(unfilteredDeploymentDescriptor, deploymentDescriptor, true,
                            filterWrappers, getEncoding(unfilteredDeploymentDescriptor));
                    // Remove the temporary file
                    FileUtils.forceDelete(unfilteredDeploymentDescriptor);
                }
                archiver.getArchiver().addFile(deploymentDescriptor, APP_CLIENT__XML);
            }

            // create archive
            archiver.createArchive(project, archive);
        } catch (ArchiverException e) {
            throw new MojoExecutionException("There was a problem creating the JEE Application Client  archive: " + e.getMessage(), e);
        } catch (ManifestException e) {
            throw new MojoExecutionException("There was a problem reading / creating the manigest for the JEE Application Client  archive: " + e.getMessage(), e);
        } catch (IOException e) {
            throw new MojoExecutionException("There was a I/O problem creating the creating the JEE Application Client  archive: " + e.getMessage(), e);
        } catch (DependencyResolutionRequiredException e) {
            throw new MojoExecutionException("There was a problem resolving dependencies while creating the JEE Application Client  archive: " + e.getMessage(), e);
        } catch (MavenFilteringException e) {
            throw new MojoExecutionException("There was a problem filtering the deployment descriptor: " + e.getMessage(), e);
        }




        project.getArtifact().setFile(jarFile);



    }

    /**
     * Returns the App-client Jar file to generate.
     *
     * @param basedir    the output directory
     * @param finalName  the name of the ear file
     * @return the Application client JAR file to generate
     */
    private static File getAppClientJarFile(File basedir, String finalName) {
        return new File(basedir, finalName + ".jar");
    }

    /**
     * Get the encoding from an XML-file.
     *
     * @param xmlFile the XML-file
     * @return The encoding of the XML-file, or UTF-8 if it's not specified in the file
     * @throws IOException if an error occurred while reading the file
     */
    private String getEncoding(File xmlFile)
            throws IOException {
        XmlStreamReader xmlReader = new XmlStreamReader(xmlFile);
        return xmlReader.getEncoding();
    }
}
